package com.weavernorth.store.service;

import com.weaver.general.BaseBean;
import com.weavernorth.util.Logger4jUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;

import static com.weavernorth.util.dateUtils.dateCompare;
import static com.weavernorth.util.dateUtils.dateTimeBetween;

/**
 * @Author: BruseeWang
 * @Date: 2021/1/25 17:37
 * @Description: 巡店计划日期校验
 */
public class CheckDate_store implements Action {
    public static org.apache.log4j.Logger log = Logger4jUtil.getLoggerByName("/Timmer/storeClockInTimmer/");
    @Override
    public String execute(RequestInfo requestInfo) {
        log.info("巡店计划流程时间校验开始：");
        RecordSet rsTable=new RecordSet();
        RecordSet rsDetail=new RecordSet();
        //获取请求ID
        String strLcId = requestInfo.getRequestid();
        log.info("requestid的值是：" + strLcId);

        String strTableName ="";
        String strSqlMain="select tablename  from workflow_bill where id = (select formid from workflow_base where id=(select workflowid from workflow_requestbase  where requestid='"+strLcId+"') and isbill=1)";
        log.info("查询主表数据的sql：" + strSqlMain);
        //查询主表数据的sql
        rsTable.execute(strSqlMain);
        if(rsTable.next()){
            strTableName=rsTable.getString("tablename");
        }

        log.info("tablename的值是：" + strTableName);
        String strBdId = requestInfo.getWorkflowid();
        log.info("workflowid的值是：" + strBdId);
        //查询明细表数据的sql
        String strSqlDetail = "select hr.lastname,fmd.* from " + strTableName + " fm," + strTableName + "_dt1 fmd,hrmresource hr  where fmd.mainid=fm.id AND hr.id = fmd.detail_resourceId and  fm.requestid = '" + strLcId + "'";
        log.info("查询明细表数据的sql：" + strSqlDetail);
        rsDetail.executeQuery(strSqlDetail);
        List<String> list=new ArrayList<String>();
        while (rsDetail.next()){
            String strUser= Util.null2String(rsDetail.getString("detail_resourceId"));

            String strLastName=Util.null2String(rsDetail.getString("lastname"));
            //巡店日期
            String strXdDate= Util.null2String(rsDetail.getString("xdrq"));
            //巡店时间
            String strXdsj= Util.null2String(rsDetail.getString("xdsj"));
            list.add(strUser+","+strLastName+","+strXdDate);
        }
        log.info("获取表单数据："+list.toString());
        String strCode="";
        String strMess="";
        String strResult;
        try {
            strResult=checkDate(list,strTableName,strLcId);
            log.info("检验结果："+strResult);
            strCode=strResult.split("\\|")[0].toString();
            strMess=strResult.split("\\|")[1].toString();
        } catch (Exception e) {
            e.printStackTrace();
            log.info("日期对比出现异常："+e.toString());
            requestInfo.getRequestManager().setMessagecontent("日期对比出现异常："+e.toString());
            return FAILURE_AND_CONTINUE;
        }
        //如果存在时间交叉,则不允许提交
        if (strCode.equals("1")){
            log.info(strMess);
            requestInfo.getRequestManager().setMessagecontent(strMess);
            return FAILURE_AND_CONTINUE;
        }else if (strCode.equals("2")){
            log.info("程序异常,异常消息："+strMess);
            requestInfo.getRequestManager().setMessagecontent("程序异常,异常消息："+strMess);
            return FAILURE_AND_CONTINUE;
        }
        return SUCCESS;
    }
    public static String checkDate(List<String> list, String strTable, String requestid) {
        log.info("数据集合："+list.toString());
        log.info("requestid="+requestid);
        String result="0|无交叉日期数据";
        BaseBean bb=new BaseBean();
        if (list.size()>0){
            for (String s : list) {
                String usrid=s.split(",")[0];
                String usrName=s.split(",")[1];
                String dates=s.split(",")[2];
                RecordSet rsNew=new RecordSet();
                //获取某个人所有的加班申请数据
                String strSql = "select fmd.* from " + strTable + " fm," + strTable + "_dt1 fmd,workflow_requestbase wr where fm.id=fmd.mainid and fmd.detail_resourceId="+usrid+" and wr.requestid=fm.requestid and wr.currentnodetype!='0' and fmd.xdrq='"+dates+"' and fm.requestid!='"+requestid+"'";
                log.info("查询巡店计划历史记录sql:"+strSql);
                try {
                    rsNew.executeQuery(strSql);
                    while (rsNew.next()) {
                      result = "1|人员" + usrName+"在日期："+dates+"已存在记录,不允许重复提交！";
                      log.info(result.toString());
                    }
                }catch (Exception ex){
                    result = "2|" + ex.toString();
                    log.info(result);
                }
            }
        }
        return result;
    }
}
